<?php

namespace app\admin\controller;



class Upload extends Common
{
	//
	public function index(){
		$func = input('func');
		$path = input('path');
		$image_upload_limit_size = config('constant.limit_size');
		$info = array(
				'num'=>request()->input('num'),
				'title' => '',
				'upload' =>url('upload/imageUp').'?path='.$path,
				'fileList'=>url('upload/fileList').'?path='.$path,
				'size' => $image_upload_limit_size/(1024 * 1024).'M',
				'type' =>'jpg,png,gif,jpeg',
				'input' => request()->input('input'),
				'func' => empty($func) ? 'undefined' : $func,
		);
		return view("index",['info'=>$info]);
	}
	/**
	 * @function imageUp
	 */
	public function imageUp()
	{
		$uploadpath = input('path');
		// 获取表单上传文件
		$file = request()->file('file');
		// 移动到框架应用根目录/uploads/ 目录下
		$info = $file->move( './uploads');
		if($info){
			$path=str_replace("\\","/",$info->getSaveName());
			$return_data['url'] = WEB_SITE.'/uploads/'.$path;
			$return_data['title'] = '标题';
			$return_data['original'] = ''; // 这里好像没啥用 暂时注释起来
			$return_data['state'] = 'SUCCESS';
			$return_data['path'] = '/uploads/'.$path;;
			return  json_encode($return_data);
		}else{
			// 上传失败获取错误信息
			echo $file->getError();
		}
	}
	//获得图片列表
	public function fileList(){

		/* 判断类型 */
		$type = input('type','Images');
		switch ($type){
			/* 列出图片 */
			case 'Images' : $allowFiles = 'png|jpg|jpeg|gif|bmp';break;
			case 'Flash' : $allowFiles = 'flash|swf';break;
			/* 列出文件 */
			default : $allowFiles = '.+';
		}
		$path = 'uploads/';
		is_dir($path) || mkdir($path, 0777, true);
		$listSize = 100000;

		$key = empty($_GET['key']) ? '' : $_GET['key'];

		/* 获取参数 */
		$size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
		$start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
		$end = $start + $size;
		/* 获取文件列表 */
		$files = $this->getfiles($path, $allowFiles, $key);
		if (!count($files)) {
			echo json_encode(array(
					"state" => "没有相关文件",
					"list" => array(),
					"start" => $start,
					"total" => count($files)
			));
			exit;
		}

		/* 获取指定范围的列表 */
		$len = count($files);
		for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--){
			$list[] = $files[$i];
		}

		/* 返回数据 */
		$result = json_encode(array(
				"state" => "SUCCESS",
				"list" => $list,
				"start" => $start,
				"total" => count($files)
		));

		echo $result;
	}
	/**
	 * 遍历获取目录下的指定类型的文件
	 * @param $path
	 * @param array $files
	 * @return array
	 */
	private function getfiles($path, $allowFiles, $key, &$files = array()){
		if (!is_dir($path)) return null;
		if(substr($path, strlen($path) - 1) != '/') $path .= '/';
		$handle = opendir($path);
		while (false !== ($file = readdir($handle))) {
			if ($file != '.' && $file != '..') {
				$path2 = $path . $file;
				if (is_dir($path2)) {
					$this->getfiles($path2, $allowFiles, $key, $files);
				} else {
					if (preg_match("/\.(".$allowFiles.")$/i", $file) && preg_match("/.*". $key .".*/i", $file)) {
						$files[] = array(
								'url'=> '/'.$path2,
								'name'=> $file,
								'mtime'=> filemtime($path2)
						);
					}
				}
			}
		}
		return $files;
	}
	/*
          删除上传的图片
 */
	public function delupload(){
		$action = request()->input('action','del');
		$filename= request()->input('filename');
		$filename= empty($filename) ?  request()->input('url') : $filename;
		$filename= str_replace('../','',$filename);
		$filename= trim($filename,'.');
		$filename= trim($filename,'/');
		if($action=='del' && !empty($filename) && file_exists($filename)){
			$size = getimagesize($filename);
			$filetype = explode('/',$size['mime']);
			if($filetype[0]!='image'){
				exit;
			}
			if(unlink($filename)){
				echo 1;
			}else{
				echo 0;
			}
			exit;
		}
	}
}
